﻿using System.Collections;
using System.Data;
using WHC.Security.Entity;
using WHC.Security.IDAL;
using System;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Collections.Generic;
using WHC.Security.Common;
using WHC.OrderWater.Commons;


namespace WHC.Security.DALSQL
{
    public class Function : BaseDAL<FunctionInfo>, IFunction
    {
        #region 对象实例及构造函数

        public static Function Instance
        {
            get
            {
                return new Function();
            }
        }
        public Function()
            : base("T_ACL_Function", "ID")
        {
        }

        #endregion

        /// <summary>
        /// 将DataReader的属性值转化为实体类的属性值，返回实体类
        /// </summary>
        /// <param name="dr">有效的DataReader对象</param>
        /// <returns>实体类对象</returns>
        protected override FunctionInfo DataReaderToEntity(IDataReader dataReader)
        {
            FunctionInfo functionInfo = new FunctionInfo();
            SmartDataReader reader = new SmartDataReader(dataReader);

            functionInfo.ID = reader.GetInt32("ID");
            functionInfo.PID = reader.GetInt32("PID");
            functionInfo.Name = reader.GetString("Name");
            functionInfo.ControlID = reader.GetString("ControlID");
            functionInfo.SystemType_ID = reader.GetString("SystemType_ID");

            return functionInfo;
        }

        /// <summary>
        /// 将实体对象的属性值转化为Hashtable对应的键值
        /// </summary>
        /// <param name="obj">有效的实体对象</param>
        /// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(FunctionInfo obj)
        {
            FunctionInfo info = obj as FunctionInfo;
            Hashtable hash = new Hashtable();

            hash.Add("PID", info.PID);
            hash.Add("Name", info.Name);
            hash.Add("ControlID", info.ControlID);
            hash.Add("SystemType_ID", info.SystemType_ID);

            return hash;
        }

		public new bool  DeleteByKey(string key)
		{
			FunctionInfo info = (FunctionInfo) this.FindByID(key);
            if (info != null)
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand command = null;

                using (DbConnection connnection = db.CreateConnection())
                {
                    connnection.Open();
                    DbTransaction transaction = connnection.BeginTransaction();
                    try
                    {
                        string commandText = string.Format("UPDATE [T_ACL_Function] SET PID={0} Where PID={1}", info.PID, key);
                        command = db.GetSqlStringCommand(commandText);
                        db.ExecuteNonQuery(command, transaction);

                        commandText = "Delete From [T_ACL_Function] Where ID =" + key;
                        command = db.GetSqlStringCommand(commandText);
                        db.ExecuteNonQuery(command, transaction);

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
			return true;
		}

		public List<FunctionInfo> GetFunctions(string roleIDs, string typeID)
		{
            string commandText = @"SELECT DISTINCT [ID],PID,[NAME],ControlID,SystemType_ID FROM [T_ACL_Function] 
            INNER JOIN T_ACL_Role_Function On [T_ACL_Function].ID=T_ACL_Role_Function.Function_ID WHERE Role_ID IN (" + roleIDs + ")";
			if (typeID.Length > 0)
			{
				commandText = commandText + " AND SystemType_ID='" + typeID + "'";
			}
            List<FunctionInfo> list = new List<FunctionInfo>();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(commandText);

			using (IDataReader dataReader = db.ExecuteReader(command))
			{
				while (dataReader.Read())
				{
					list.Add(this.DataReaderToEntity(dataReader));
				}
				dataReader.Close();
			}
			return list;
		}

        public List<FunctionInfo> GetFunctionsByRole(int roleID)
		{
            string commandText = @"SELECT * FROM [T_ACL_Function] 
            LEFT JOIN T_ACL_Role_Function On [T_ACL_Function].ID=T_ACL_Role_Function.Function_ID WHERE Role_ID = " + roleID;
            List<FunctionInfo> list = new List<FunctionInfo>();

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(commandText);

			using (IDataReader dataReader = db.ExecuteReader(command))
			{
				while (dataReader.Read())
				{
					list.Add(this.DataReaderToEntity(dataReader));
				}
				dataReader.Close();
			}
			return list;
		}

        public List<FunctionInfo> GetAllFirstLevelFunctions()
        {
            string commandText = @"SELECT * FROM [T_ACL_Function] WHERE [T_ACL_Function].pID = -1";
            List<FunctionInfo> list = new List<FunctionInfo>();

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(commandText);

            using (IDataReader dataReader = db.ExecuteReader(command))
            {
                while (dataReader.Read())
                {
                    list.Add(this.DataReaderToEntity(dataReader));
                }
                dataReader.Close();
            }
            return list;
        }
        public List<FunctionInfo> GetAllFunctions()
        {
            string commandText = @"SELECT * FROM [T_ACL_Function]";
            List<FunctionInfo> list = new List<FunctionInfo>();

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(commandText);

            using (IDataReader dataReader = db.ExecuteReader(command))
            {
                while (dataReader.Read())
                {
                    list.Add(this.DataReaderToEntity(dataReader));
                }
                dataReader.Close();
            }
            return list;
        }
	}
}